home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / prg_bar.exe / TPROGBAR.CPP < prev    next >
C/C++ Source or Header  |  1992-08-29  |  5KB  |  190 lines

  1. #define Uses_TStaticText
  2. #define Uses_TStatusDef
  3. #define Uses_TStatusItem
  4. #define Uses_TStatusLine
  5. #define Uses_TEventQueue
  6. #define Uses_TRect
  7. #define Uses_TDeskTop
  8. #define Uses_TView
  9. #define Uses_TWindow
  10. #define Uses_TDialog
  11. #define Uses_TButton
  12. #define Uses_StaticText
  13. #define Uses_TSItem
  14. #define Uses_TEvent
  15. #define Uses_TKeys
  16. #define Uses_TDrawBuffer
  17.  
  18. #include <tv.h>
  19. __link( RView )
  20. __link( RDialog )
  21. __link( RButton )
  22. #include <mem.h>                       // memset
  23.  
  24. #include <dos.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <ctype.h>
  28. #include "tprogbar.h"
  29.  
  30. const int sampleIterations = 21 ;
  31.  
  32.  
  33. TProgressBar::TProgressBar(TRect& r, unsigned long iters ) : TView ( r )
  34. {
  35.      options |= ofSelectable;
  36.      eventMask = (evKeyboard | evBroadcast);
  37.      maxWidth   = DISPLAYLEN ;//- 1 ;  // newWidth;         // maximum width of thermometer bar
  38.      curWidth   = 0;                    // current width of percentage bar
  39.      oldWidth   = 0;                    // old width of percentage bar
  40.      chPercent  = 100.0/maxWidth;       // percent per character
  41.      maxIter    = iters ;               // maximum iteration
  42.      curIter    = 0;                    // current iteration
  43.      oldPercent = 0;                    // old percentage
  44.     curPercent = 0;                    // current percentage
  45.     backChar   = '░';                  // background character
  46.     percChar   = '▓';                  // foreground character
  47.     bar = new char[ maxWidth + 1];
  48.     memset( bar, backChar, maxWidth );
  49.      bar[ maxWidth ] = '\0';
  50. }
  51.  
  52. TProgressBar::~TProgressBar()
  53. {
  54.      delete bar ;
  55. }
  56.  
  57. void TProgressBar::handleEvent(TEvent& event)
  58. {
  59.     TView::handleEvent(event);
  60.  
  61.     switch(event.what)
  62.     {
  63.     case evKeyboard:
  64.           mainProcess() ;
  65.           break;
  66.      case evBroadcast:
  67.           if(event.message.command == cmOK)
  68.                 mainProcess() ;
  69.           break;
  70.      }
  71.      clearEvent(event);
  72. }
  73.  
  74. void TProgressBar::draw()
  75. {
  76.     char color = getColor(1);
  77.     int i;
  78.     TDrawBuffer nbuf;
  79.     char Buf[DISPLAYLEN+1];
  80.  
  81.      nbuf.moveChar(0,' ',color,size.x);
  82.  
  83.      nbuf.moveStr( 0, bar, color ) ;
  84.  
  85.     writeLine(0, 0, size.x, 1, nbuf);
  86. }
  87.  
  88. void TProgressBar::mainProcess( void )
  89. {
  90.      unsigned long cnt = 0 ;
  91.      for( int x = 0; x < sampleIterations; x++ )
  92.      {
  93.           setCurIter( ++cnt );         // set the current iteration count & update
  94.           delay(500);
  95.      }
  96.      delay(500);
  97.      message(owner,evCommand,cmOK,this);// close dialog box
  98. }
  99.  
  100. void TProgressBar::calcPercent ( )
  101. {
  102.      unsigned int percent;
  103.      unsigned int width;
  104.  
  105.     // calculate the new percentage
  106.     percent = (int) ( ( (double)curIter / (double)maxIter ) * (double)100 );
  107.  
  108.     // percentage change?
  109.     if ( percent != curPercent )
  110.     {
  111.         oldPercent = curPercent;       // save current percentage
  112.         curPercent = percent;          // save new percentage
  113.         width = curPercent / chPercent;// calculate percentage bar width
  114.  
  115.         // width change?
  116.         if ( width != curWidth )
  117.           {
  118.             oldWidth = curWidth;       // save the current width
  119.                 curWidth = width;          // save new width
  120.  
  121.             // update the bar string
  122.                 if ( oldWidth < curWidth )
  123.             {
  124.                 for ( int i = oldWidth; i < curWidth; i++ )
  125.                 {
  126.                     bar[i] = percChar;
  127.                 }
  128.             }
  129.             else
  130.             {
  131.                 for ( int i = curWidth; i < oldWidth; i++ )
  132.                      {
  133.                     bar[i] = backChar;
  134.                 }
  135.                 }
  136.         }
  137.     }
  138. }
  139.  
  140. // terminate the thermometer bar display
  141. void TProgressBar::term ( )
  142. {
  143. }
  144.  
  145. // return the maximum iteration
  146. unsigned long TProgressBar::getMaxIter ( )
  147. {
  148.     return ( maxIter );
  149. }
  150.  
  151. // return the current iteration
  152. unsigned long TProgressBar::getCurIter ( )
  153. {
  154.      return ( curIter );
  155. }
  156.  
  157. // set a new maximum iteration & update display
  158. void TProgressBar::setMaxIter ( unsigned long newMax )
  159. {
  160.     unsigned long tmp = maxIter;
  161.     maxIter = newMax;
  162.      memset( bar, backChar, maxWidth );
  163.      curWidth   = 0;                    // current width of percentage bar
  164.      oldWidth   = 0;                    // old width of percentage bar
  165.      curIter    = 0;                    // current iteration
  166.      oldPercent = 0;                    // old percentage
  167.     curPercent = 0;                    // current percentage
  168.      if ( tmp )                // since it starts with 0, only update if changing
  169.     {
  170.           drawView();                       // update the thermometer bar display
  171.     }
  172. }
  173.  
  174. // set a new current iteration & update display
  175. void TProgressBar::setCurIter ( unsigned long newCur )
  176. {
  177.     unsigned int percent;
  178.     unsigned int width;
  179.  
  180.     curIter = newCur;
  181.  
  182.     calcPercent();
  183.  
  184.      // width change?
  185.      if ( curPercent != oldPercent )
  186.      {
  187.           drawView();                       // paint the thermometer bar
  188.      }
  189. }
  190.